package Test;

import java.util.Scanner;

public class test08 {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();

        while(t--!=0){
            int n = scan.nextInt();
            int k = scan.nextInt();
            char[] s = scan.next().toCharArray();
            int ret = 0;
             int tmp = 0;
            for(int i=0;i<s.length;i++){
                if(s[i]=='L') {
                    ret--;
                     tmp = 0;
                }
                else {
                     tmp++;
                     if(tmp==3) {
                         ret+=k;
                         tmp = 0;
                     }
                     else{
                         ret++;
                     }
//                    if(i-2>=0 && s[i-2]=='W' && s[i-1]=='W') ret+=k;
//                    else ret+=1;
                }
            }
            System.out.println(ret);
        }
    }
}
